package org.adream.account.service;

import java.util.List;

import javax.servlet.http.HttpSession;

import org.adream.account.dao.BusinessScaleDao;
import org.adream.account.entity.BusinessScaleEntity;
import org.adream.account.model.ResultModel;
import org.adream.account.util.Constant;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class BusinessScaleService {
	
	private final static Logger logger = LoggerFactory.getLogger(BusinessScaleService.class);
	
	@Autowired
	private BusinessScaleDao scaleDao;
	
	@Autowired
	private HttpSession session;
	
	/**
	 * 新增
	 * @param scale
	 */
	public ResultModel<String> addScale(BusinessScaleEntity scale) {
		String operator = (String) session.getAttribute("uid");
		scale.setCreator(operator);
		scale.setModifier(operator);
		scale.setDr(Constant.ONE);
		int result = scaleDao.addScale(scale);
		if(result == 0) {
			logger.warn("新增企业规模数据失败,数据库错误,bsName:{}", scale.getBsName());
			return new ResultModel<String>(ResultModel.ERR_SYS, "系统错误,请联系系统管理员", null);
		}
		return new ResultModel<String>(ResultModel.SUCCESS, ResultModel.SUCCESS_MSG, null);
	}
	
	/**
	 * 删除 逻辑删除
	 * @param bsId
	 */
	public ResultModel<String> deleteScale(Integer bsId) {
		BusinessScaleEntity scale = scaleDao.queryScaleByBsidAndDr(bsId, Constant.ONE);
		if(scale == null) {
			logger.warn("删除企业规模失败,数据不存在,bsId:{}", bsId);
			return new ResultModel<String>(ResultModel.NOT_FOUND_OBJECT, "删除失败,数据不存在", null);
		}
		String modifier = (String)session.getAttribute("uid");
		scale.setModifier(modifier);
		scale.setDr(Constant.ZERO);
		int result = scaleDao.deleteScale(scale);
		if(result == 0) {
			logger.warn("删除企业规模数据失败,数据库错误,bsId:{}", bsId);
			return new ResultModel<String>(ResultModel.ERR_SYS, "系统错误,请联系系统管理员", null);
		}
		return new ResultModel<String>(ResultModel.SUCCESS, ResultModel.SUCCESS_MSG, null);
	}
	
	/**
	 * 更新
	 * @param bsId
	 * @param scaleEntity
	 */
	public ResultModel<String> updateScale(Integer bsId,BusinessScaleEntity scaleEntity) {
		BusinessScaleEntity scale = scaleDao.queryScaleByBsidAndDr(bsId, Constant.ONE);
		if(scale == null) {
			logger.warn("更新企业规模失败,数据不存在,bsId:{}", bsId);
			return new ResultModel<String>(ResultModel.NOT_FOUND_OBJECT, "更新失败,数据不存在", null);
		}
		String modifier = (String) session.getAttribute("uid");
		scale.setBsName(scaleEntity.getBsName());
		scale.setModifier(modifier);
		int result = scaleDao.updateScale(scale);
		if(result == 0) {
			logger.warn("更新企业规模数据失败,数据库错误,bsId:{}", bsId);
			return new ResultModel<String>(ResultModel.ERR_SYS, "系统错误,请联系系统管理员", null);
		}
		return new ResultModel<String>(ResultModel.SUCCESS, ResultModel.SUCCESS_MSG, null);
	}
	
	/**
	 * 根据删除标志查询
	 * @param dr 删除标志
	 * @return
	 */
	public List<BusinessScaleEntity> queryScaleByDr(Integer dr) {
		return scaleDao.queryScalesByDr(dr);
	}
	
	/**
	 * 根据bsId,删除标志查询
	 * @param bsId
	 * @param dr
	 * @return
	 */
	public BusinessScaleEntity queryScaleByBsIdAndDr(Integer bsId,Integer dr) {
		return scaleDao.queryScaleByBsidAndDr(bsId, dr);
	}
}